package gui;

import gui.AddRemoveSinger.addSinger;
import gui.AddRemoveSinger.deSinger;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class AddRemoveSinger extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1472277497115633194L;
	private Manager manager;
	// private JFrame main;
	private JButton deSinger, addSinger;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField nameText;
	private JTextField upcText;

	public AddRemoveSinger(Manager manager) {
		this.manager = manager;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();

	}

	private void addPanel() {
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(6, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		adder.add(new JLabel("UPC of Item:"));
		upcText = new JTextField();
		upcText.setPreferredSize(new Dimension(300, 30));
		adder.add(upcText);

		adder.add(new JLabel("Name of Singer:"));
		nameText = new JTextField();
		adder.add(nameText);
		
		adder.add(new JLabel("Click to insert Singer"));
		addSinger = new JButton("add");
		addSinger.addActionListener(new addSinger());
		adder.add(addSinger);

		adder.add(new JLabel("Click to remove Singer"));
		deSinger = new JButton("remove");
		deSinger.addActionListener(new deSinger());
		adder.add(deSinger);

		add(adder);
	}

	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED),
				"Confirmation");
		conPanel.setBorder(title);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		confirmation = new JScrollPane(textArea);
		confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		conPanel.add(confirmation, BorderLayout.CENTER);

		add(conPanel);
	}

	public class addSinger implements ActionListener {
		String upc, name;

		public void actionPerformed(ActionEvent arg0) {
			upc = upcText.getText();
			name = nameText.getText();
			try {
				manager.insertSinger(upc, name);
				textArea.append("******************* Singer Inserted ******************" + "\n" +
								"\n" + "Name of Singer: " + name +
								"\n" + "UPC of Item: " + upc +
								"\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Singer already exists.  Cannot be added!");
				e.printStackTrace();
			}

		}
	}

	public class deSinger implements ActionListener {
		String upc, name;

		public void actionPerformed(ActionEvent arg0) {
			upc = upcText.getText();
			name = nameText.getText();
			
			try {
				manager.deleteSinger(name, upc);
				textArea.append("******************* Singer Deleted ******************" + "\n"
								+ "\n" + "Singer by the name: " + name + " has been removed successfully"
								+ "\n" + "All associated Items removed."
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Singer does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}

	}
}
